package com.ddyx.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.common.util.EncrypMD5;
import com.ddyx.common.util.LoginResponse;
import com.ddyx.entity.User;
import com.ddyx.mapper.UserMapper;
import com.ddyx.service.IUserService;

import net.sf.json.JSONObject;

@Service("userService")
public class UserServiceImpl implements IUserService {

	@Resource
	UserMapper userMapper;
	@Override
	public String checkUser(String userName, String password) {
		// TODO Auto-generated method stub	
	//	Map<String, Object> map = new HashMap<String,Object>();
		try {
	//now		List<Map<String,Object>>  selectUser = userMapper.selectUser(userName);
			User selectUser = userMapper.selectUser(userName);
			if(selectUser==null){
		//		User user = new User();
		//		user.setUserName(userName);
	//			map.put("status", false);
	//			map.put("message", "用户名不存在");
	//			map.put("data", selectUser);
	//			return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
				return LoginResponse.getInstance().setStatus(false)
								.setMessage("用户名不存在")
								.setData(new JSONObject()).toString();
			}else {
				String pwd = new EncrypMD5().EncoderByMd5(password) ;
				User checkuser = userMapper.getUser(userName, pwd);
				if(checkuser==null) {
			//		User user = new User();
			//		user.setUserName(userName);
			//		user.setPassword(password);
					return LoginResponse.getInstance().setStatus(false)
									.setMessage("用户名或密码错误")
									.setData(new JSONObject()).toString();
				}else {
					checkuser.setPassword(password);
					return LoginResponse.getInstance().setStatus(true)
									.setMessage("登陆成功")
									.setData(checkuser).toString();
				}
				}
		}catch(Exception e){
			return LoginResponse.getInstance().setStatus(false)
							.setMessage(e.toString())
							.toString();
		}
	}
}
